package com.yzl.meeting.business.dao.impl;

import com.yzl.meeting.business.dao.RolePermissionDao;
import com.yzl.meeting.business.entity.Permission;
import com.yzl.meeting.business.entity.Role;
import com.yzl.meeting.business.utils.MyDbUtils;
import com.yzl.meeting.business.vo.RolePerVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Title: RolePermissionDaoImpl
 * @Author: 姚志立
 * @Description:
 * @Date: 2020/10/12 16:44
 * @Version: 1.0
 */
public class RolePermissionDaoImpl implements RolePermissionDao {
    private QueryRunner qr = MyDbUtils.getQr();

    @Override
    public int addPer(Permission permission) throws SQLException {
        String sql = "insert into t_perm (perName,perDes) values (?,?)";
        String sql2 = "select last_insert_id() from t_perm";
        Object[] params = {permission.getPerName(), permission.getPerDes()};
        qr.update(sql, params);
        String s = qr.query(sql2, new ScalarHandler()).toString();
        return Integer.valueOf(s);
    }

    @Override
    public int addRole(RolePerVo rolePerVo) throws Exception {
        Connection conn = MyDbUtils.getConnection();
        String sql = "insert into t_role (roleName,roleDescription) values (?,?)";
        String sql2 = "select @@identity";
        Object[] params = {rolePerVo.getRoleName(), rolePerVo.getRoleDescription()};
        qr.update(conn,sql, params);
        BigInteger i = (BigInteger) qr.query(conn,sql2, new ScalarHandler(1));
        return i.intValue();
    }

    @Override
    public List<Role> selectAllRole() throws Exception {
        String sql = "select * from t_role";
        List<Role> query = qr.query(sql, new BeanListHandler<Role>(Role.class));
        return query;
    }

    @Override
    public List<Permission> selectAllPer() throws Exception {
        String sql = "select * from t_perm";
        List<Permission> query = qr.query(sql, new BeanListHandler<Permission>(Permission.class));
        return query;
    }

    @Override
    public void addRolePer(int roleId, Integer perId) throws Exception {
        Connection conn = MyDbUtils.getConnection();
        String sql = "insert into t_role_perm values (?,?)";
        Object[] params = {roleId, perId};
        qr.update(conn,sql, params);
    }


    @Override
    public void deletePer(Integer perId) throws SQLException {
        String sql = "delete from t_perm where perId=?";
        Object[] params = {perId};
        qr.update(sql, params);
    }

    @Override
    public void deleteRolePer(Integer roleId) throws SQLException {
        String sql = "delete from t_role_perm where roleId=?";
        Object[] params = {roleId};
        qr.update(sql, params);
    }

    @Override
    public void deleteRole(Integer roleId) throws SQLException {
        String sql = "delete from t_role where roleId=?";
        Object[] params = {roleId};
        qr.update(sql, params);
    }
}
